package com.lovo.system.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.lovo.system.dto.CustomerDto;
import com.lovo.system.mapper.CustomerMapper;
import com.lovo.system.service.ICustomerService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class CustomerServiceImpl extends ServiceImpl<CustomerMapper, CustomerDto> implements ICustomerService {

    @Autowired
    CustomerMapper customerMapper;

    @Override
    public IPage<CustomerDto> cutByItem(int curPage, int pageSize, String name, String range) {
        QueryWrapper qw = new QueryWrapper();
        if (name != null && !"".equals(name)) {
            qw.like("customer_name", name);
        }
        if (range != null && !"".equals(range)) {
            qw.like("nature_of_business", range);
        }
        return customerMapper.selectPage(new Page<>(curPage, pageSize), qw);
    }

    @Override
    public String addCustomer(CustomerDto customerDto) {
        try {
            customerMapper.insert(customerDto);
            return "添加成功";
        } catch (Exception e) {
            return "添加失败";
        }
    }

    @Override
    public String deleteCustomer(int id) {
        try {
            customerMapper.deleteById(id);
            return "删除成功";
        } catch (Exception e) {
            return "删除失败";
        }
    }

    @Override
    public String updateCustomer(CustomerDto customerDto) {
        try {
            customerMapper.updateById(customerDto);
            return "修改成功";
        } catch (Exception e) {
            return "修改失败";
        }
    }

    @Override
    public CustomerDto queryOneById(int id) {
        return customerMapper.selectById(id);
    }
}
